﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RuiXinCoffee.DAL;
using RuiXinCoffee.Model;
using RuiXinCoffee.Utils;

namespace RuiXinCoffee.BLL
{
    internal class ProductService
    {
        private ProductDAO productDAO = new ProductDAO();
        
        //获取所有上架咖啡
        public List<Product> GetAllProducts()
        {
            try
            {
                return productDAO.GetAllProducts();
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取咖啡列表失败", ex);
            }
        }
        
        //根据分类获取
        public List<Product> GetProductsByCategory(int categoryId)
        {
            try
            {
                return productDAO.GetProductsByCategory(categoryId);
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取分类失败", ex);
            }
        }
        
        //管理员，获取所有（包括未上架的）
        public List<Product> GetAllProductsForAdmin()
        {
            try
            {
                return productDAO.GetAllProductsForAdmin();
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取管理员咖啡列表失败", ex);
            }
        }
        
        //根据ID获取
        public Product GetProductById(int id)
        {
            try
            {
                return productDAO.GetProductById(id);
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取咖啡详情失败", ex);
            }
        }
        
        //管理员，添加
        public bool AddProduct(Product product)
        {
            try
            {
                //验证
                ValidateProduct(product);
                
                int result = productDAO.AddProduct(product);
                return result > 0;
            }
            catch (UserOperationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new BusinessException("添加咖啡失败", ex);
            }
        }
        
        //管理员，更新
        public bool UpdateProduct(Product product)
        {
            try
            {
                ValidateProduct(product);
                
                int result = productDAO.UpdateProduct(product);
                return result > 0;
            }
            catch (UserOperationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new BusinessException("更新咖啡失败", ex);
            }
        }
        
        //管理员，删除
        public bool DeleteProduct(int id)
        {
            try
            {
                int result = productDAO.DeleteProduct(id);
                return result > 0;
            }
            catch (Exception ex)
            {
                throw new DatabaseException("删除咖啡失败", ex);
            }
        }
        
        //管理员，上架/下架
        public bool ToggleProductStatus(int id, bool isOnSale)
        {
            try
            {
                Product product = productDAO.GetProductById(id);
                if (product == null)
                {
                    throw new BusinessException($"ID为{id}的咖啡不存在");
                }
                
                product.IsOnSale = isOnSale;
                int result = productDAO.UpdateProduct(product);
                return result > 0;
            }
            catch (Exception ex)
            {
                throw new BusinessException("更改咖啡状态失败", ex);
            }
        }

        //获取所有的分类，对于咖啡而言，主要是指美式、拿铁、小黄油等（主要是参考瑞幸那个）
        //在数据库里用一张分类表，存储所有的分类信息，把分类ID作为外键关联到产品表
        public DataTable GetAllCategories()
        {
            try
            {
                return productDAO.GetAllCategories();
            }
            catch (Exception ex)
            {
                throw new DatabaseException("获取分类列表失败", ex);
            }
        }
        
        //验证咖啡参数是否合理
        private void ValidateProduct(Product product)
        {
            if (string.IsNullOrWhiteSpace(product.Name))
            {
                throw new UserOperationException("咖啡名称不能为空");
            }
            
            if (product.Price <= 0)
            {
                throw new UserOperationException("咖啡价格必须大于0");
            }
            
            if (product.CategoryId <= 0)
            {
                throw new UserOperationException("必须选择一分类");
            }
        }
    }
}
